<template>
	<br>
	<input id="i1" type="text" placeholder="请输入用户名" v-model="user.username">
	<input id="i2" type="password" placeholder="请输入密码" v-model="user.password">
	<br>
	<button @click="login" plain="true" type="primary" style="width: 200px;">点我登录</button>
	<br>
	<view style="width: 300px;margin: 0 auto;">
		<checkbox style="transform:scale(0.7);" v-model="isChecked" @click="checkbox"/>我已阅读并同意
		<router-link to="/pages/policy/policy" style="text-decoration: none; color: #fccb30;">《用户隐私政策》</router-link>
	</view>
</template>


<script setup>
import {ref, vModelCheckbox} from "vue";
import {logintoindex} from "@/api/login.js"
const isChecked = ref(false);
const user = ref({
  username: '',
  password: '',
})
const checkbox = () => {
	isChecked.value = !isChecked.value;
}
const login =async () => {
	console.log(isChecked.value);
	if (isChecked.value==false){
		uni.showModal({
			title: "提示",
			content: "请同意用户隐私政策",
			showCancel: false,// 只显示“确定”按钮
		});
	}else {
		console.log(user.value);
		let res = await logintoindex(user.value);
		localStorage.setItem('token',res.data.token);
		console.log(res);
		console.log(res.data);
		// localStorage.setItem('user',JSON.stringify(res.data);
		localStorage.setItem('userid',res.data.id);
		// localStorage.setItem('user',Object.keys(res.data)
	 //  .map(key => `${key}=${encodeURIComponent(res.data[key])}`)
	 //  .join('&'));
		uni.showModal({
			title: "提示",
			content: "登陆成功",
			showCancel: false,// 只显示“确定”按钮
			success(res) {
				uni.switchTab({
					url: "/pages/index/index"
				})
			},
		});
	};
}
</script>

<style>
	#i1,#i2 {
		border-bottom: 1px solid black;
		width: 200px; height: 40px;margin: 0 auto;
		margin-top: 10px;
	}
</style>
